package com.wuda.servlet.user;

import com.wuda.bean.User;
import com.wuda.dao.UserDao;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/user/getUserDetail")
public class GetUserDetail extends HttpServlet {

	private static final long serialVersionUID = 1L;
	UserDao userDao = new UserDao();

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("application/json; charset=utf8");
		PrintWriter out = response.getWriter();

		int userId = Integer.parseInt(request.getParameter("userId"));

		User user = userDao.getUserById(userId);

		JSONObject jsonResult = new JSONObject();
		if (user != null) {
			jsonResult.put("code", 200);
			jsonResult.put("msg", "查询成功");

			JSONObject userData = new JSONObject();
			userData.put("userId", user.getUserId());
			userData.put("username", user.getUsername());
			userData.put("email", user.getEmail());

			jsonResult.put("data", userData);


		} else {
			jsonResult.put("code", 204);
			jsonResult.put("msg", "用户不存在");
		}

		out.print(jsonResult);
		out.flush();
	}
}
